package ee.ttu.lecture;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PageAssembler
{
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	public PageAssembler(HttpServletRequest req, HttpServletResponse resp)
	{
		this.request = req;
		this.response = resp;
		resp.setContentType("text/html");
		resp.setCharacterEncoding("UTF-8");
	}
	
	public void assembleLectureView() throws IOException, ServletException
	{
		RequestDispatcher dispatcher;
		
		dispatcher = request.getRequestDispatcher("header.jsp");
		if(dispatcher != null) dispatcher.include(request, response);
		
		dispatcher = request.getRequestDispatcher("lecture.jsp");
		if(dispatcher != null) dispatcher.include(request, response);
		
		dispatcher = request.getRequestDispatcher("footer.jsp");
		if(dispatcher != null) dispatcher.include(request, response);
	}
	
	public void assembleMainView() throws IOException, ServletException
	{
		RequestDispatcher dispatcher;
		
		dispatcher = request.getRequestDispatcher("header.jsp");
		if(dispatcher != null) dispatcher.include(request, response);
		
		DataAccess da = DataAccess.getDataAccess();
		response.getWriter().println(UserInterface.generateLectureTable(da.getLectures()));
		response.getWriter().println(UserInterface.generateUsersTable(da.getUsers()));
		
		dispatcher = request.getRequestDispatcher("footer.jsp");
		if(dispatcher != null) dispatcher.include(request, response);
	}
}
